﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SevenZip;
using System.IO;

namespace LuaBoard
{
    public partial class ProgressForm : Form
    {
        private delegate void InvokeNumber(int value);
        public string filePath;
        public string outPath;
        public bool ret = false;

        public ProgressForm(int min, int max)
        {
            InitializeComponent();
            progressBar1.Maximum = max;
            progressBar1.Minimum = min;
            progressBar1.Value = 0;
            
        }
        public ProgressForm(string FilePath, string OutPath)
        {
            InitializeComponent();
            progressBar1.Maximum = 100;
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;
            filePath = FilePath;
            outPath = OutPath;
        }

        private void ProgressForm_Load(object sender, EventArgs e)
        {
            if (filePath != null)
            {
                ret = Compress7Z();
                if (ret)
                    this.DialogResult = DialogResult.OK;
                
            }
            this.Close();
        }
        public void SetProgressBarValue(int value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new InvokeNumber(this.SetProgressBarValue), new object[] { value });
            }
            else
            {
                this.progressBar1.Value = value;
                this.Refresh();
            }
        }
        public bool Compress7Z()
        {
            bool IsDir = true;
            if (File.Exists(filePath)) IsDir = false;
            SevenZipCompressor.SetLibraryPath(Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"7z.dll"));
            SevenZipCompressor cmp = new SevenZipCompressor
            {
                ArchiveFormat = OutArchiveFormat.SevenZip,
                CompressionMethod = CompressionMethod.Lzma,
                CompressionMode = CompressionMode.Create,
                CompressionLevel = Archiver.C7Z.compressionLevel,
                VolumeSize = Archiver.C7Z.volumeSize
            };
            //cmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>(cmp_FileCompressionStarted);
            //cmp.Compressing += new EventHandler<ProgressEventArgs>(cmp_Compressing);
            //cmp.FileCompressionFinished+=new EventHandler<EventArgs>(cmp_FileCompressionFinished);
            
            try
            {
                if (IsDir)
                    cmp.CompressDirectory(filePath, outPath, true);
                else
                    cmp.CompressFiles(outPath, filePath);

            }
            catch
            {
                return false;
            }

            return true;

        }
        private void cmp_FileCompressionStarted(object sender, FileNameEventArgs e)
        {
            this.Text = e.FileName;
            SetProgressBarValue((int)e.PercentDone);
        }
        private void cmp_Compressing(object sender, ProgressEventArgs e)
        {
            SetProgressBarValue((int)e.PercentDone);
        }
        private void cmp_FileCompressionFinished(object sender, EventArgs e)
        {
        }
    }
}
